package com.example.anliku.controller;

import com.alibaba.fastjson.JSON;
import com.example.anliku.entity.Example;
import com.example.anliku.entity.ExampleForES;
import com.example.anliku.entity.Message;
import com.example.anliku.entity.PageQuery;
import com.example.anliku.mapper.ExampleClazzMapper;
import com.example.anliku.mapper.ExampleKnowledgeMapper;
import com.example.anliku.mapper.ExampleMapper;
import com.example.anliku.service.ElasticSearchService;
import com.example.anliku.service.ExampleService;
import com.example.anliku.service.LuceneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/lucene")
@Api(tags = "案例查找模块")
public class LuceneController {

    @Resource
    private LuceneService luceneService;

    @Resource
    private ExampleMapper exampleMapper;

    @Resource
    private ElasticSearchService elasticSearchService;

    @Resource
    private ExampleKnowledgeMapper exampleKnowledgeMapper;

    @Resource
    private ExampleClazzMapper exampleClazzMapper;



//    @ApiOperation(value = "创建")
//    @GetMapping("/create")
//    public String creates() throws IOException {
//        luceneService.createProductIndex(exampleMapper.selectAll());
//        return "hello world" ;
//    }
//    @ApiOperation(value = "查找")
//    @GetMapping("/find")
//    public PageInfo<HashMap> find(Example pro) throws Exception{
//        PageInfo pageInfo = new PageInfo();
//        pageInfo.setPageNum(pro.getPage());
//        pageInfo.setPageSize(pro.getLimit());
//        PageQuery pageQuery = new PageQuery();
//        pageQuery.setPageInfo(pageInfo);
//        pageQuery.setParams(pro);
//        PageQuery result  = luceneService.searchProduct(pageQuery);
//
//        PageInfo<HashMap> pageResult = new PageInfo<HashMap>();
//        pageResult.setPageSize(pro.getLimit());
//        pageResult.setPageNum(pro.getPage());
//        pageResult.setTotal(result.getPageInfo().getTotal());
//        pageResult.setList(result.getResults());
//        return pageResult;
//    }

    @ApiOperation(value = "ElasticSearch 定制查找")
    @GetMapping("/select")
    public Message select (Integer page, Integer size, String keyword, Integer dec){
        return Message.success("查询成功").add(elasticSearchService.select(page,size,keyword,dec));
    }

    @ApiOperation(value = "根据id查询信息")
    @GetMapping("/selectone")
    public Message selectone (Integer exampleId){
        return Message.success("查询成功").add(elasticSearchService.selectById(exampleId));
    }

    @ApiOperation(value = "索引一键清除")
    @DeleteMapping("/del")
    public Message del (){
        elasticSearchService.allDelete();
        return Message.success("删除成功");
    }

    @ApiOperation(value = "根据id删除单个索引")
    @DeleteMapping("/delone")
    public Message delone (Integer exampleId){
        return Message.success("删除成功").add(elasticSearchService.delete(exampleId));
    }

    @ApiOperation(value = "redis数据同步")
    @PostMapping("/redis")
    public Message redis (){
        return elasticSearchService.blukUpadteForRedis()?Message.success(null):Message.fail(null);
    }


    @ApiOperation(value = "根据数据库创建文档")
    @PostMapping ("/creates")
    public Message create(){
        List<Example> exampleList =  exampleMapper.selectAll();
        for(Example example : exampleList) {
            ExampleForES exampleForES = new ExampleForES();
            exampleForES.setExampleId(example.getExampleId());
            exampleForES.setExampleName(example.getExampleName());
            exampleForES.setExampleStartTime(example.getExampleStartTime());
            exampleForES.setExampleState(example.getExampleState());
            exampleForES.setExampleTypeName(example.getExampleTypeName());
            exampleForES.setExampleKnowledgeId(JSON.toJSONString(exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(example.getExampleId())));
            exampleForES.setExampleClazz(JSON.toJSONString(exampleClazzMapper.selectAllByExampleClazzExampleId(example.getExampleId())));
            exampleForES.setExampleImage(example.getExampleImage());
            exampleForES.setExampleIntroduction(example.getExampleIntroduction());
            exampleForES.setExampleUserName(example.getExampleUserName());
            exampleForES.setExamplePublicState(example.getExamplePublicState());
            exampleForES.setExampleDownload(0);
            elasticSearchService.insert(exampleForES);
        }
        return Message.success(null);
    }


}
